/*
	javascript functions for word Calculator page
*/
$(document).ready
(
	function () 
	{
		//hide areas of pages not needed on the beginning
		$('.CCalculator').hide();	
		$("#ITextAreaCalculatorResults").hide();
		$("#ICellResult").hide();
		$('#IDivSetTripDistance').hide();
		$('#IDivShowResults').hide();

		//hide the request for turning javascript off if it is on
		$('#IJavascriptOff').hide();
		
		//size up the menu home item to make the user know he/she is on homepage
		$('#IHomeToggle').css("font-size","20px");
		
		//show contents form the Calculator div
		$('.CCalculator').css("color","white");
		$('#ICalculatorDescriptionToggle').css("color","red");
		$("#IPetrolPrice").attr("hidden", false); 
		$("#IDivSetPetrolSpent").attr("hidden", true);
		$("#IPetrolPerHundredKM").attr("hidden", false);
		$("#ITripDistance").attr("hidden", false);
		$("#ITripOrigin").attr("hidden", false);
		$("#ITripDestination").attr("hidden", false);
		$("#IButtonCalculate").attr("hidden", false);
		$("#ITextAreaCalculatorResults").attr("hidden", false);

		
 		$(document).keydown
		(
			function(event)
			{
				//alert(event.which);

				/*
					left 37
					up 38
					right 39
					down 40
					h - left - 72
					j - down - 74
					k - up   - 75
					l - right - 76
					back - b - 66
					resert - r - 82
					forward - f - 70
					calculator - c - 67
					enter - 13
				*/
				if( (event.which == 116) ) 
				{
					//f5
					location.reload();	
				}
				else if( (event.which == 27) ) 
				{
					//unfocus
					$(document.activeElement).blur();
					return false;

				}
				else if( (event.which == 72) || (event.which == 66) ) 
				{
					//back
					event.preventDefault();	
						if($('#IDivSetPetrolSpent').is(':visible') )
				{
					$('#IDivSetPetrolSpent').hide();
					$('#IDivSetPetrolPrice').show();
				}
				else if($('#IDivSetTripDistance').is(':visible') )
				{
					$('#IDivSetTripDistance').hide();
					$('#IDivSetPetrolSpent').show();
				}
				else if($('#IDivShowResults').is(':visible') )
				{
					$('#IDivShowResults').hide();
					$('#IDivSetTripDistance').show();
				}	
								
					return false;

				}
				else if(event.which == 37) 
				{
					//back with arrow key
					if(!($('#IFieldPriceSetting').is(':focus') || 
					$('#IFieldPetrolSpentSetting').is(':focus') || 
					$('#IFieldTripDistanceSetting' ).is(':focus')))
					{
							if($('#IDivSetPetrolSpent').is(':visible') )
							{
								$('#IDivSetPetrolSpent').hide();
								$('#IDivSetPetrolPrice').show();
							}
							else if($('#IDivSetTripDistance').is(':visible') )
							{
								$('#IDivSetTripDistance').hide();
								$('#IDivSetPetrolSpent').show();
							}
							else if($('#IDivShowResults').is(':visible') )
							{
								$('#IDivShowResults').hide();
								$('#IDivSetTripDistance').show();
							}
					}
				}
				else if((event.which == 76) || (event.which == 70) || (event.which == 13)) 
				{
					//forward
					event.preventDefault();	
					
					if($('#IDivSetPetrolPrice').is(':visible') )
					{
						if( ( parseFloat( $('#IFieldPriceSetting').val() ).toFixed(3) 	) >= 0.001) 
						{
							$('#IDivPetrolPrice').text($('#IFieldPriceSetting').val()+' ');
							$('#IDivSetPetrolPrice').hide();
							$('#IDivSetPetrolSpent').show();
							$('#IFieldPetrolSpentSetting').show().focus();
							return false;
						}
						return false;
					} 
					else if($('#IDivSetPetrolSpent').is(':visible') )
					{
						if( ( parseFloat( $('#IFieldPetrolSpentSetting').val() ).toFixed(2) 	) >= 0.1) 
						{
							$('#IDivPetrolOverHundredKM').text($('#IFieldPetrolSpentSetting').val()+' ');
							$('#IDivSetPetrolSpent').hide();
							$('#IDivSetTripDistance').show();
								$('#IFieldTripDistanceSetting').show().focus();
							return false;
						}
						return false;
					} 
					else if($('#IDivSetTripDistance').is(':visible') )
					{
						$('.CForward').click();
					}
					return false;
				}
				else if(event.which == 39) 
				{
					//forward with arrow key	
					
					if(!($('#IFieldPriceSetting').is(':focus') || 
					$('#IFieldPetrolSpentSetting').is(':focus') || 
					$('#IFieldTripDistanceSetting' ).is(':focus')))
					{
						$('.CForward').click();
					}
				}
				else if( (event.which == 40) || (event.which == 74) || (event.which == 82) )
				{
					//reset
					event.preventDefault();	
					$('.CReset').click();
					return false;
				}	
				else if( (event.which == 38) || (event.which == 75) || (event.which == 67) )
				{
					//CCalculator
					event.preventDefault();	

					if( $('#IDivPetrolOverHundredKM').is(':visible'))
					{
						$('#IHomeToggle').click();
					}
					else
					{
						 $('#ICalculatorToggle').click();
					}
					return false;
				}
				else
				{
					if(($('#IFieldPriceSetting').is(':focus') || 
					$('#IFieldPetrolSpentSetting').is(':focus') || 
					$('#IFieldTripDistanceSetting' ).is(':focus')))
					{
						if
						(
							( (event.which>=48) && (event.which<=57) ) ||
							( (event.which>=96) && (event.which<=105) )||
							(event.which==190) || (event.which==110) || (event.which==27)
						)
							return true;
						else
							return false;
					}
					else
						return true;	
					
				}
			}
		);


		//manage the menu navigation
		$('#IHomeToggle').click
		(
			function()
			{
				$('#IHomeToggle').css("font-size","20px");
				$('#ICalculatorToggle').css("font-size","18px");
				$('.CCalculator').hide();
				$('.CHome').show();				
 				return false;
			}
		);
		
		$('#ICalculatorToggle').click
		(
			function()
			{
				$('#IHomeToggle').css("font-size","18px");
				$('#ICalculatorToggle').css("font-size","20px");
				$('.CHome').hide();
				$('.CCalculator').show();				
				
				$('#IFieldPriceSetting').show().focus();

 				return false;
			}
		);
		
		//manage the Calculator description toggle
		$('#ICalculatorDescriptionToggle').click
		(
			function()
			{
				switch(this.text)
				{
					case "Hide this description.":
					{
						$('#ICalculatorDescription').hide();
						this.text = "Show the description.";
						break;
					}					
					case "Show the description.":
					{
						$('#ICalculatorDescription').show();
						this.text = "Hide this description.";
						break;
					}
					default:
					{
						//
					}					
 				}
				return false;
			}
		);
		
		$('.CReset').click
		(
			function()
			{
				$('#IFieldPriceSetting').val(' ');
				$('#IFieldPetrolSpentSetting').val(' ');
				$('#IFieldTripDistanceSetting').val('');
				
				$('#IDivPetrolPrice').text("0.000") ;
				$('#IDivPetrolOverHundredKM').text("0.0");
				$('#IDivTripDistance').text("0");
				
				$('#ILabelLitersSpent').text(' ');
				$('#ILabelCost').text(' ');

				$('#IDivSetPetrolPrice').show();
				$('#IDivSetPetrolSpent').hide();
				$('#IDivSetTripDistance').hide();
				$('#IDivShowResults').hide();	
				
				$('#IFieldPriceSetting').show().focus();

				return false;
			}
		);
		
		$('.CForward').click
		(
			function()
			{
				if($('#IDivSetPetrolPrice').is(':visible') )
				{
					if( ( parseFloat( $('#IFieldPriceSetting').val() ).toFixed(3) 	) >= 0.001) 
					{
						$('#IDivPetrolPrice').text($('#IFieldPriceSetting').val()+' ');
						$('#IDivSetPetrolPrice').hide();
						$('#IDivSetPetrolSpent').show();
						$('#IFieldPetrolSpentSetting').show().focus();
						return false;
					}
					return false;
				} 
				else if($('#IDivSetPetrolSpent').is(':visible') )
				{
					if( ( parseFloat( $('#IFieldPetrolSpentSetting').val() ).toFixed(2) 	) >= 0.1) 
					{
						$('#IDivPetrolOverHundredKM').text($('#IFieldPetrolSpentSetting').val()+' ');
						$('#IDivSetPetrolSpent').hide();
						$('#IDivSetTripDistance').show();
						$('#IFieldTripDistanceSetting').show().focus();
						return false;
					}
					return false;
				} 
				else if($('#IDivSetTripDistance').is(':visible') )
				{
					if( ( parseFloat( $('#IFieldTripDistanceSetting').val() ).toFixed(2) 	) >= 1) 
					{
						$('#IDivTripDistance').text($('#IFieldTripDistanceSetting').val()+' ');
						$('#IDivSetTripDistance').hide();

						var tripDistanceSettingText = $("#IDivTripDistance").text();
						var tripDistance = parseInt(tripDistanceSettingText);

						var petrolSpentText = $("#IDivPetrolOverHundredKM").text();
						var petrolSpent = parseFloat(petrolSpentText);
						
						var priceText = $("#IDivPetrolPrice").text();
						var price = parseFloat(priceText);
						
						var willSpendOnTrip = (tripDistance*petrolSpent)/100;

						var tripCost = willSpendOnTrip * price;

						

						$('#ILabelLitersSpent').text(willSpendOnTrip +' ');
						$('#ILabelCost').text(tripCost+' ');

						$('#IDivShowResults').show();

					}
				}			
				return false;
			}
		);
		
		$('.CBack').click
		(
			function()
			{
				if($('#IDivSetPetrolSpent').is(':visible') )
				{
					$('#IDivSetPetrolSpent').hide();
					$('#IDivSetPetrolPrice').show();
				}
				else if($('#IDivSetTripDistance').is(':visible') )
				{
					$('#IDivSetTripDistance').hide();
					$('#IDivSetPetrolSpent').show();
				}
				else if($('#IDivShowResults').is(':visible') )
				{
					$('#IDivShowResults').hide();
					$('#IDivSetTripDistance').show();
				}

				return false;
			}
		);
	}
);
